<template>
  <div class="material-info-manage">
    <el-card shadow="hover">
      <div class="info-header">
        <el-input
          v-model="searchText"
          placeholder="搜索物料名称/编码"
          clearable
          style="width:200px; margin-right:16px;"
          @keyup.enter="doSearch"
        />
        <el-select v-model="searchType" placeholder="物料类型" clearable style="width:130px; margin-right:16px;">
          <el-option v-for="item in typeOptions" :key="item" :label="item" :value="item" />
        </el-select>
        <el-button type="primary" @click="handleAdd">新增物料</el-button>
      </div>
      <el-table :data="filteredList" border style="margin-top:16px;">
        <el-table-column prop="code" label="物料编码" width="120px"/>
        <el-table-column prop="name" label="物料名称" width="140px"/>
        <el-table-column prop="type" label="物料类型" width="120px"/>
        <el-table-column prop="unit" label="单位" width="100px"/>
        <el-table-column prop="model" label="规格型号" width="120px"/>
        <el-table-column prop="quantity" label="库存数量" width="100px"/>
        <el-table-column prop="supplier" label="供应商" width="120px"/>
        <el-table-column prop="remark" label="备注" width="180px"/>
        <el-table-column label="操作" width="180px" align="center">
          <template #default="scope">
            <el-button size="small" type="primary" text @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" text @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="420px" destroy-on-close>
      <el-form :model="editRow" :rules="formRules" ref="formRef" label-width="90px">
        <el-form-item label="物料编码" prop="code">
          <el-input v-model="editRow.code" />
        </el-form-item>
        <el-form-item label="物料名称" prop="name">
          <el-input v-model="editRow.name" />
        </el-form-item>
        <el-form-item label="物料类型" prop="type">
          <el-select v-model="editRow.type" placeholder="请选择">
            <el-option v-for="item in typeOptions" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-select v-model="editRow.unit" placeholder="请选择">
            <el-option v-for="item in unitOptions" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="规格型号" prop="model">
          <el-input v-model="editRow.model" />
        </el-form-item>
        <el-form-item label="库存数量" prop="quantity">
          <el-input-number v-model="editRow.quantity" :min="0" style="width:100%;"/>
        </el-form-item>
        <el-form-item label="供应商" prop="supplier">
          <el-input v-model="editRow.supplier" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="editRow.remark" type="textarea"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="submitEdit">保存</el-button>
      </template>
    </el-dialog>

    <!-- 删除弹窗 -->
    <el-dialog v-model="deleteVisible" title="提示" width="320px" center>
      <div>确定要删除该物料吗？</div>
      <template #footer>
        <el-button @click="deleteVisible=false">取消</el-button>
        <el-button type="danger" @click="confirmDelete">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'

// 类型定义
interface MaterialItem {
  code: string
  name: string
  type: string
  unit: string
  model: string
  quantity: number
  supplier: string
  remark: string
}

// 下拉选项
const typeOptions = ['钢筋', '水泥', '玻璃', '石膏板', '铜线', '防水涂料']
const unitOptions = ['千克', '吨', '立方米', '米', '毫米', '平方米', '个', '套', '小时']

// 模拟数据
const materialList = ref<MaterialItem[]>([
  { code: 'M001', name: 'HRB400钢筋', type: '钢筋', unit: '吨', model: 'φ12', quantity: 120, supplier: '宝钢', remark: '' },
  { code: 'M002', name: 'PO42.5水泥', type: '水泥', unit: '吨', model: '50kg/袋', quantity: 200, supplier: '海螺', remark: '' },
  { code: 'M003', name: '钢化玻璃', type: '玻璃', unit: '平方米', model: '5+12A+5', quantity: 80, supplier: '耀皮', remark: '透明' },
  { code: 'M004', name: '石膏板', type: '石膏板', unit: '平方米', model: '9.5mm', quantity: 150, supplier: '龙牌', remark: '' },
  { code: 'M005', name: 'BV铜线', type: '铜线', unit: '米', model: '2.5mm²', quantity: 1000, supplier: '远东电缆', remark: '' },
  { code: 'M006', name: '防水涂料', type: '防水涂料', unit: '千克', model: '20kg/桶', quantity: 60, supplier: '东方雨虹', remark: '卫生间专用' }
])

// 搜索
const searchText = ref('')
const searchType = ref('')
const filteredList = computed(() => {
  let arr = materialList.value
  if (searchText.value) {
    arr = arr.filter(
      r =>
        r.name.includes(searchText.value) ||
        r.code.includes(searchText.value)
    )
  }
  if (searchType.value) {
    arr = arr.filter(r => r.type === searchType.value)
  }
  return arr
})
function doSearch() {}

// 新增/编辑弹窗
const dialogVisible = ref(false)
const dialogTitle = ref('新增物料')
const editRow = reactive<MaterialItem>({
  code: '',
  name: '',
  type: '',
  unit: '',
  model: '',
  quantity: 0,
  supplier: '',
  remark: ''
})
const formRef = ref()
const formRules = {
  code: [{ required: true, message: '必填', trigger: 'blur' }],
  name: [{ required: true, message: '必填', trigger: 'blur' }],
  type: [{ required: true, message: '必选', trigger: 'change' }],
  unit: [{ required: true, message: '必选', trigger: 'change' }]
}

function handleAdd() {
  dialogTitle.value = '新增物料'
  Object.assign(editRow, { code: '', name: '', type: '', unit: '', model: '', quantity: 0, supplier: '', remark: '' })
  dialogVisible.value = true
}
function handleEdit(row: MaterialItem) {
  dialogTitle.value = '编辑物料'
  Object.assign(editRow, row)
  dialogVisible.value = true
}
function submitEdit() {
  formRef.value?.validate((valid: boolean) => {
    if (!valid) return
    if (dialogTitle.value.startsWith('编辑')) {
      const idx = materialList.value.findIndex(t => t.code === editRow.code)
      if (idx !== -1) materialList.value[idx] = { ...editRow }
      ElMessage.success('编辑成功')
    } else {
      materialList.value.unshift({ ...editRow })
      ElMessage.success('新增成功')
    }
    dialogVisible.value = false
  })
}

// 删除
const deleteVisible = ref(false)
const deleteTarget = ref<MaterialItem | null>(null)
function handleDelete(row: MaterialItem) {
  deleteTarget.value = row
  deleteVisible.value = true
}
function confirmDelete() {
  if (deleteTarget.value) {
    materialList.value = materialList.value.filter(t => t.code !== deleteTarget.value!.code)
    ElMessage.success('删除成功')
  }
  deleteVisible.value = false
}
</script>

<style scoped>
.material-info-manage {
  width: 100%;
  margin: 0;
  padding: 32px 0;
}
.info-header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}
</style>
